<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4.延迟消失</title>
  <style>
    .box {
      width: 800px;
      margin: 50px auto;
    }

    .box>div {
      float: left;
    }

    #first-menu {
      width: 300px;
      height: 200px;
      background-color: aqua;
      line-height: 200px;
      text-align: center;
      font-weight: 700;
    }

    #second-menu {
      width: 200px;
      height: 100px;
      background-color: yellow;
      text-align: center;
      margin-left: 30px;
      font-weight: 700;
      display: none;
    }
  </style>
</head>

<body>
  <div class="box">
    <div id="first-menu">一级菜单</div>
    <div id="second-menu">二级菜单</div>
  </div>
</body>
<script>
  var firstMenu = document.getElementById('first-menu')
  var secondMenu = document.getElementById('second-menu')

  var timeId
  firstMenu.onmouseout = secondMenu.onmouseout = function () {
    timeId = setTimeout(function () {
      secondMenu.style.display = 'none'
    }, 1000)
  }
  firstMenu.onmouseover = secondMenu.onmouseover = function () {
    clearTimeout(timeId)
    secondMenu.style.display = 'block'
  }
</script>

</html>